/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gcliff.action.glpks;

import com.gcliff.dao.glpks.CheckAppStatusDAO;
import com.gcliff.dao.glpks.ProcessUserLoginDAO;
import com.gcliff.model.glpks.Sso;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author krishna Code belongs to GCliff.
 */
public class GlobalSessionActions extends ActionSupport implements SessionAware {
    //

    protected static final Log log = LogFactory.getLog(GlobalSessionActions.class);
    static ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"classpath:appContextWeb.xml"});
    static BeanFactory factory = (BeanFactory) appContext;
    // DAO Objects
    private CheckAppStatusDAO checkAppStatusDAO = (CheckAppStatusDAO) factory.getBean("checkAppStatusDAO");
    private ProcessUserLoginDAO processUserLoginDAO = (ProcessUserLoginDAO) factory.getBean("processUserLoginDAO");
    // Session Perameters
    private Map session;

    public Map getSession() {
        return session;
    }

    public void setSession(Map session) {
        this.session = session;
    }
    // Global Variables
    private Map<String, Object> resultSetMap;
    private List<String> actionMessage = new ArrayList<String>();
    private List<String> errorMessage = new ArrayList<String>();

    public List<String> getActionMessage() {
        return actionMessage;
    }

    public void setActionMessage(List<String> actionMessage) {
        this.actionMessage = actionMessage;
    }

    public List<String> getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(List<String> errorMessage) {
        this.errorMessage = errorMessage;
    }

    //Main Method
    public static void main(String[] args) {
        GlobalSessionActions gsa = new GlobalSessionActions();
        gsa.processUserLogin();
    }

    // Default Execute Method
    @Override
    public String execute() {
        if (getSession().isEmpty()) {
            errorMessage.add("Athentication failed.");
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    // Get Login Page Method
    public String getLoginPage() {
        return signOut();
    }

    // Sign Out Method
    public String signOut() {
        getSession().clear();
        return SUCCESS;
    }
    // User Login Process check
    private String userName;
    private String userPassword;
    private List<Sso> ssoList;
    private Sso ssoUI;

    public String processUserLogin() {
        resultSetMap = processUserLoginDAO.processUserLogin(userName, userPassword);
        //Logic
        ssoList = (List<Sso>) resultSetMap.get("PO_RESULT_SET");

        switch (ssoList.size()) {
            case 1:
                getSession().put("SESSION_LOGIN_USER_OBJECT", ssoList.get(0));
                ssoUI = (Sso) getSession().get("SESSION_LOGIN_USER_OBJECT");
                actionMessage.add("Login Successfull, you have been mapped with " + ssoUI.getRoleName() + " role.");
                return SUCCESS;
            case 0:
                errorMessage.add("Athentication failed.");
                return INPUT;
            default:
                errorMessage.add("Unknown state");
                return ERROR;
        }

    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public List<Sso> getSsoList() {
        return ssoList;
    }

    public void setSsoList(List<Sso> ssoList) {
        this.ssoList = ssoList;
    }

    public Sso getSsoUI() {
        return ssoUI;
    }

    public void setSsoUI(Sso ssoUI) {
        this.ssoUI = ssoUI;
    }
}
